<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/build/css/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/my-style.css">
</head>
<style>
     .laydate-btns-clear {
        display: none!important;
    }
</style>

<body>
<div class="kit-doc">
    <!--表格搜索框组-->
    <div class="layui-form-item layui-formgroup layui-tab-card">

        <!-- 下拉框-->
        <div class="layui-inline">
            <form class="layui-form " action="">
                <label class="layui-form-label">请选择统计条件：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="projectId" placeholder="项目编号">
                </div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="projectName" placeholder="项目名称">
                </div>
<!--                <div class="layui-input-inline">-->
<!--                    <select id="typeName" lay-filter="aihao">-->
<!--                        <option value="">项目类型</option>-->
<!--                        <option value="0" th:each="type:${typeList}"-->
<!--                                th:value="${type.getTypeName()}" th:text="${type.getTypeName()}">-->
<!--                            模块一-->
<!--                        </option>-->
<!--                    </select>-->
<!--                </div>-->

                <!-- 时间 -->
                <div class="layui-inline">
                    <input type="hidden" id="mon">
                    <input type="hidden" id="sun">
                    <div class="layui-input-inline">
                        <input class="layui-input" id="test6" placeholder=" 时间段 " type="text">
                    </div>
                </div>
            </form>
        </div>

        <!-- 按钮 -->
        <button class="layui-btn layui-btn-sm" id="search">统计</button>
        <button class="layui-btn layui-btn-sm" id="export">导出</button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="preWeekBtn">
            <i class="fa fa-reply margin-r3"></i>上一周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="thisWeekBtn">
            <i class="fa fa-search margin-r3"></i>本周
        </button>
        <button class="layui-btn layui-btn-primary layui-btn-sm" id="nextWeekBtn">
            <i class="fa fa-share margin-r3"></i>下一周
        </button>
    </div>

    <div class="layui-card layui-card-body">
        <table class="layui-table xuhao-table" id="table-context" lay-filter="test">
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            </script>
        </table>
    </div>
    <!--<div class="layui-card layui-card-body">-->
    <!--<div id="chartsone" style="height: 400px;"></div>-->
    <!--</div>-->

</div>
<!-- .js-->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script src="/plugins/layui/layui.js"></script>
<script src="/js/workhour.js"></script>
<script src="/js/ry-ui.js"></script>
<script>
    var now = new Date();

    // 获取周一和周末
    var start = getTime(0, now);
    var end = getTime(-6, now);
    $("#mon").val(start);
    $("#sun").val(end);
    $("#test6").val(start + ' - ' + end);
</script>
<script>
    layui.use(['table', 'jquery'], function () {
        var table = layui.table;
        var $ = layui.$;
        var projectTable = table.render({
            elem: '#table-context'
            , url: '/statistics/project/list'
            , height: 'full-200'
            , page: true
            , limit: 10
            , limits: [10, 20, 30]
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , cols: [
                [
                    // {type: 'checkbox'},
                    {field: 'projectId', title: '项目编号', align: 'center',width: '25%'},
                    {field: 'projectName', title: '项目名', align: 'center',  width: '30%'},
                    // {
                    //     field: 'projectType',
                    //     title: '项目类型',
                    //     align: 'center',  width: '20%'
                    // },
                     {field: 'workhour', title: '填报工时', sort: true,  align: 'center', width: '15%'},
                     {field: 'realworkhour', title: '已审批工时', sort: true,  align: 'center', width: '15%'}
                    , {fixed: 'right', title: "操作", align: 'center', toolbar: '#barDemo',  width: '15%'}
                ]
            ],
            done: function (res, curr, count) {
            }
        });


        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                var startToEnd = $("#test6").val().split(" - ");
                $.modal.open("详情", "/statistics/project/view/" + data.projectId + "?projectName=" + data.projectName + "&startDate=" + startToEnd[0] + "&endDate=" + startToEnd[1]);
            }
        });

        /**
         * 刷新表格数据
         */
        function reloadData() {
            var startToEnd = $("#test6").val().split(" - ");
            var projectId = $("#projectId").val();
            var projectName = $("#projectName").val();
            // var typeName = $("#typeName").val();
            // console.log(startToEnd,projectId,projectName,typeName);
            projectTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    projectId: projectId,
                    projectName: projectName,
                    // projectType: typeName,
                    startDate: startToEnd[0],
                    endDate: startToEnd[1]
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }

        // 搜索
        $("#search").click(function () {
            reloadData();
        });

        // 导出
        $("#export").click(function () {
            var startToEnd = $("#test6").val().split(" - ");
            var projectId = $("#projectId").val();
            var projectName = $("#projectName").val();
            var typeName = $("#typeName").val();
            window.location.href = "/statistics/project/excel?" +
                "projectId=" + projectId +
                "&projectName=" + projectName +
                "&projectType=" + typeName +
                "&startDate=" + startToEnd[0] +
                "&endDate=" + startToEnd[1];
        });

        // 上一周
        $("#preWeekBtn").click(function () {
            var monDate = $("#mon").val();
            var preMon = dateToStr(addDate(monDate, -7));
            var preSun = dateToStr(addDate(monDate, -1));
            $("#mon").val(preMon);
            $("#sun").val(preSun);
            $("#test6").val(preMon + ' - ' + preSun);
            reloadData();
        });

        // 下一周
        $("#nextWeekBtn").click(function () {
            var monDate = $("#mon").val();
            var nextMon = dateToStr(addDate(monDate, 7));
            var nextSun = dateToStr(addDate(monDate, 13));
            $("#mon").val(nextMon);
            $("#sun").val(nextSun);
            $("#test6").val(nextMon + ' - ' + nextSun);
            reloadData();
        });

        // 本周一周
        $("#thisWeekBtn").click(function () {
            var now = new Date();
            // 获取周一和周末
            var start = getTime(0, now);
            var end = getTime(-6, now);
            $("#mon").val(start);
            $("#sun").val(end);
            $("#test6").val(start + ' - ' + end);
            reloadData();
        });
    });
</script>
</body>
</html>